home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Scrollbar.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  5.2 KB  |  197 lines

  1. //
  2. // "$Id: Fl_Scrollbar.cxx,v 1.7.2.1 1999/05/14 09:07:07 bill Exp $"
  3. //
  4. // Scroll bar widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Scrollbar.H>
  28. #include <FL/fl_draw.H>
  29. #include <math.h>
  30.  
  31. #define INITIALREPEAT .5
  32. #define REPEAT .05
  33.  
  34. void Fl_Scrollbar::increment_cb() {
  35.   handle_drag(clamp(value() + (
  36.     ((pushed_>1) == (maximum()>=minimum())) ? linesize_ : -linesize_)));
  37. }
  38.  
  39. void Fl_Scrollbar::timeout_cb(void* v) {
  40.   Fl_Scrollbar* s = (Fl_Scrollbar*)v;
  41.   s->increment_cb();
  42.   Fl::add_timeout(REPEAT, timeout_cb, s);
  43. }
  44.  
  45. int Fl_Scrollbar::handle(int event) {
  46.   if (!pushed_) {
  47.     if (horizontal()) {
  48.       if (w() < 3*h()) return Fl_Slider::handle(event);
  49.       if (Fl_Slider::handle(event, x()+h(), y(), w()-2*h(), h())) return 1;
  50.     } else {
  51.       if (h() < 3*w()) return Fl_Slider::handle(event);
  52.       if (Fl_Slider::handle(event, x(), y()+w(), w(), h()-2*w())) return 1;
  53.     }
  54.   }
  55.   switch (event) {
  56.   case FL_RELEASE:
  57.     if (pushed_) {
  58.       Fl::remove_timeout(timeout_cb, this);
  59.       pushed_ = 0;
  60.       redraw();
  61.     }
  62.     handle_release();
  63.     return 1;
  64.   case FL_PUSH:
  65.     if (horizontal()) {
  66.       if (Fl::event_inside(x(), y(), h(), h())) pushed_ = 1;
  67.       if (Fl::event_inside(x()+w()-h(), y(), h(), h())) pushed_ = 2;
  68.     } else {
  69.       if (Fl::event_inside(x(), y(), w(), w())) pushed_ = 1;
  70.       if (Fl::event_inside(x(), y()+h()-w(), w(), w())) pushed_ = 2;
  71.     }
  72.     if (pushed_) {
  73.       handle_push();
  74.       Fl::add_timeout(INITIALREPEAT, timeout_cb, this);
  75.       increment_cb();
  76.       redraw();
  77.     }
  78.     return 1;
  79.   case FL_DRAG:
  80.     return pushed_;
  81.   case FL_SHORTCUT: {
  82.     int v = value();
  83.     int ls = maximum()>=minimum() ? linesize_ : -linesize_;
  84.     if (horizontal()) {
  85.       switch (Fl::event_key()) {
  86.       case FL_Left:
  87.     v -= ls;
  88.     break;
  89.       case FL_Right:
  90.     v += ls;
  91.     break;
  92.       default:
  93.     return 0;
  94.       }
  95.     } else { // vertical
  96.       switch (Fl::event_key()) {
  97.       case FL_Up:
  98.     v -= ls;
  99.     break;
  100.       case FL_Down:
  101.     v += ls;
  102.     break;
  103.       case FL_Page_Up:
  104.     if (slider_size() >= 1.0) return 0;
  105.     v -= int((maximum()-minimum())*slider_size()/(1.0-slider_size()));
  106.     v += ls;
  107.     break;
  108.       case FL_Page_Down:
  109.     if (slider_size() >= 1.0) return 0;
  110.     v += int((maximum()-minimum())*slider_size()/(1.0-slider_size()));
  111.     v -= ls;
  112.     break;
  113.       case FL_Home:
  114.     v = int(minimum());
  115.     break;
  116.       case FL_End:
  117.     v = int(maximum());
  118.     break;
  119.       default:
  120.     return 0;
  121.       }
  122.     }
  123.     v = int(clamp(v));
  124.     if (v != value()) {
  125.       Fl_Slider::value(v);
  126.       value_damage();
  127.       do_callback();
  128.     }
  129.     return 1;}
  130.   }
  131.   return 0;
  132. }
  133.  
  134. void Fl_Scrollbar::draw() {
  135.   if (damage()&FL_DAMAGE_ALL) draw_box();
  136.   int X = x()+Fl::box_dx(box());
  137.   int Y = y()+Fl::box_dy(box());
  138.   int W = w()-Fl::box_dw(box());
  139.   int H = h()-Fl::box_dh(box());
  140.   if (horizontal()) {
  141.     if (W < 3*H) {Fl_Slider::draw(X,Y,W,H); return;}
  142.     Fl_Slider::draw(X+H,Y,W-2*H,H);
  143.     if (damage()&FL_DAMAGE_ALL) {
  144.       draw_box((pushed_&1) ? down(slider()) : slider(),
  145.            X, Y, H, H, selection_color());
  146.       draw_box((pushed_&2) ? down(slider()) : slider(),
  147.           X+W-H, Y, H, H, selection_color());
  148.       if (active_r())
  149.         fl_color(labelcolor());
  150.       else
  151.         fl_color(inactive(labelcolor()));
  152.       int w1 = (H-1)|1; // use odd sizes only
  153.       int Y1 = Y+w1/2;
  154.       int W1 = w1/3;
  155.       int X1 = X+w1/2+W1/2;
  156.       fl_polygon(X1-W1, Y1, X1, Y1-W1, X1, Y1+W1);
  157.       X1 = X+W-(X1-X)-1;
  158.       fl_polygon(X1+W1, Y1, X1, Y1+W1, X1, Y1-W1);
  159.     }
  160.   } else { // vertical
  161.     if (H < 3*W) {Fl_Slider::draw(X,Y,W,H); return;}
  162.     Fl_Slider::draw(X,Y+W,W,H-2*W);
  163.     if (damage()&FL_DAMAGE_ALL) {
  164.       draw_box((pushed_&1) ? down(slider()) : slider(),
  165.            X, Y, W, W, selection_color());
  166.       draw_box((pushed_&2) ? down(slider()) : slider(),
  167.            X, Y+H-W, W, W, selection_color());
  168.       if (active_r())
  169.         fl_color(labelcolor());
  170.       else
  171.         fl_color(labelcolor() | 8);
  172.       int w1 = (W-1)|1; // use odd sizes only
  173.       int X1 = X+w1/2;
  174.       int W1 = w1/3;
  175.       int Y1 = Y+w1/2+W1/2;
  176.       fl_polygon(X1, Y1-W1, X1+W1, Y1, X1-W1, Y1);
  177.       Y1 = Y+H-(Y1-Y)-1;
  178.       fl_polygon(X1, Y1+W1, X1-W1, Y1, X1+W1, Y1);
  179.     }
  180.   }
  181. }
  182.  
  183. Fl_Scrollbar::Fl_Scrollbar(int X, int Y, int W, int H, const char* L)
  184.   : Fl_Slider(X, Y, W, H, L)
  185. {
  186.   box(FL_FLAT_BOX);
  187.   color(FL_DARK2);
  188.   slider(FL_UP_BOX);
  189.   linesize_ = 16;
  190.   pushed_ = 0;
  191.   step(1);
  192. }
  193.  
  194. //
  195. // End of "$Id: Fl_Scrollbar.cxx,v 1.7.2.1 1999/05/14 09:07:07 bill Exp $".
  196. //
  197.